package wf.source.hrm.service.impl;

import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import wf.source.hrm.SsoClient;
import wf.source.hrm.domain.Employee;
import wf.source.hrm.domain.Sso;
import wf.source.hrm.mapper.EmployeeMapper;
import wf.source.hrm.service.IEmployeeService;
import wf.source.hrm.util.AjaxResult;
import wf.source.hrm.util.StrUtils;
import wf.source.hrm.util.UserType;
import wf.source.hrm.util.encrypt.MD5;

import java.io.Serializable;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wangfang
 * @since 2020-06-22
 */
@Service
public class EmployeeServiceImpl extends ServiceImpl<EmployeeMapper, Employee> implements IEmployeeService {

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private SsoClient ssoClient;
    /**
     * 在操作员工信息的时候，要同步操作Sso会员
     */
    @Override
    public boolean insert(Employee entity) {
        //保存登陆信息
        Sso sso=new Sso();
        sso.setCreateTime(System.currentTimeMillis());
        sso.setPhone(entity.getTel());
        sso.setEmail(entity.getEmail());
        sso.setNickName(StrUtils.getRandomString(10));
        sso.setUsername(entity.getUsername());
        sso.setSecLevel(0);
        //若用户类型是员工
        sso.setUserType(UserType.USERTYPE_EMPLOYEE);
        //设置状态 未激活 -1， 已激活 1， 已手机实名认证 2
        sso.setBitState(1L);
        //生成盐值
        String salt = StrUtils.getComplexRandomString(32);
        sso.setSalt(salt);
        //使用MD5加密
        String secretePassword = MD5.getMD5(entity.getPassword() + salt);

        sso.setPassword(secretePassword);
        //保存sso会员信息
        AjaxResult ajaxResult = ssoClient.addOrUpate(sso);
        System.out.println(ajaxResult.getResultObj().toString());
        //获取ssoId
        Long ssoId = Long.valueOf(ajaxResult.getResultObj().toString());
        //保存员工表信息
        employeeMapper.insert(entity);
        return true;
    }

    @Override
    public boolean deleteById(Serializable id) {
        //查询对应的ssoId
        Employee employee = employeeMapper.selectById(id);
        //先删除sso表
        ssoClient.delete(employee.getSsoId());
        //删除员工表
        employeeMapper.deleteById(id);
        return true;
    }

    @Override
    public boolean updateById(Employee entity) {
        //修改员工信息
        employeeMapper.updateById(entity);
        //查询员工对应的ssoId
        Employee employee = employeeMapper.selectById(entity.getId());
        Long ssoId = employee.getSsoId();
        //保存登陆信息
        Sso sso = new Sso();
        sso.setId(ssoId);
        sso.setPhone(entity.getTel());
        sso.setCreateTime(System.currentTimeMillis());
        sso.setEmail(entity.getEmail());
        sso.setNickName(StrUtils.getRandomString(10));
        sso.setUsername(entity.getUsername());
        sso.setSecLevel(0);
        //若用户类型是员工
        sso.setUserType(UserType.USERTYPE_EMPLOYEE);
        //设置状态 未激活 -1， 已激活 1， 已手机实名认证 2
        sso.setBitState(1L);
        //生成盐值
        String salt = StrUtils.getComplexRandomString(32);
        sso.setSalt(salt);
        //使用MD5加密
        String secretePassword = MD5.getMD5(entity.getPassword() + salt);

        sso.setPassword(secretePassword);
        //保存sso会员信息
        ssoClient.addOrUpate(sso);
        return true;
    }
}
